#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
int max(int a, int b)
{
    return a > b ? a : b;
}

int TreeHigh(struct TreeNode* root)
{
    if (root == NULL)
        return 1;
    int left = TreeHigh(root->left);
    int right = TreeHigh(root->right);
    if (left == 0 || right == 0)
        return 0;
    if (abs(left - right) <= 1)
    {
        return max(left, right) + 1;
    }
    return 0;
}

bool isBalanced(struct TreeNode* root) {
    int a = TreeHigh(root);
    if (a == 0)
        return false;
    return true;
}